﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.UI;


namespace Trig_AMJ_HR_Forms.Layouts.Trig_AMJ_HR_Forms
{
    public partial class HRRequest_Print : LayoutsPageBase
    {
        SPHelper obj = new SPHelper();
        Demand_Voucher_FormClass demandVoucherclass = new Demand_Voucher_FormClass(); // Helper class for this solution
        Trig_WF trigWFClass = new Trig_WF();

        #region "Properties"

        string CurrentUser = SPContext.Current.Web.CurrentUser.LoginName;

        public static string SiteURL;
        public static string HRRequestNo;
        public static string SourceLstName;
        public static string MainLstName;
        public static string TaskLstName;
        public static string RequestTypeLstName;
        public static string AttLibName;
        public static string CmntsLstName;
        public static string WorkflowConfigList;
        public static string lstGuid { get; set; }
        public string camlQry { get; set; }
        public static string Mode { get; set; }
        public static string submittedfor { get; set; }
        string ErrorMsgs;
        public static int wfOrder = 0;
        public static string DocumentTypesList { get; set; }
        string WFCurrentOrder = string.Empty;
        string WFTaskType = string.Empty;

        public static string LeaveRefID;
        public static string refID;
        private static bool Submit = false;

        object[] ExitParams;

        private static SPListItemCollection oItemsColl = null;
        private static SPListItem oItems = null;

        public static string RequestType = null;

        string[] buttonVisibilty = new string[5];


        #endregion


        public HRRequest_Print()
        {
            SiteURL = SPContext.Current.Site.Url;
            SiteURL = SiteURL + "/sites/forms/";
            MainLstName = "HR Request Forms";
            TaskLstName = "HR Request Tasks";
            CmntsLstName = "HR Request Comments";
            SourceLstName = "Leave Request Forms";
            RequestTypeLstName = "HR Request Types";
            AttLibName = "Attachments";
            WorkflowConfigList = "Workflow Configuration List";
            DocumentTypesList = "DocumentTypes";

        }

        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                if (!IsPostBack)
                {
                    Submit = false;
                    Mode = Request.QueryString["Mode"];

                    refID = Request.QueryString["RefID"]; // From Dashboard.
                    LeaveRefID = Request.QueryString["LeaveRefID"]; // From Dashboard.

                    RequestType = null;

                    if (Mode == "Edit" || Mode == "View")
                    {
                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            ReLoadDetails();
                        });
                    }
                    else
                    {
                        pnlFullForm.Visible = false;
                        pnlAccessDenied.Visible = true;
                        lblAccessDeniedMsg.Visible = true;
                        lblAccessDeniedMsg.Text = "Unable to open the form. Value is missing. Contact administrator";
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorMsgs = "Trig_AMJ_HR_Forms :: HR Request form" + " :: " + "Parent" + " :: " + "Page_Load";
                obj.GenerateExceptionLog(ErrorMsgs, ex.Message, "");
            }

        }

        #region "Reload Forms"

        public void ReLoadDetails()
        {
            try
            {
                SPFieldUserValue fldUser;
                ArrayList _arrayList = new ArrayList();

                string RefNo=string.Empty;

                if (refID != null)
                    camlQry = "<Where><Eq><FieldRef Name=\"Reference_x0020_Number\" /><Value Type=\"Text\">" + refID + "</Value></Eq></Where>";
                if (LeaveRefID != null)
                    camlQry = "<Where><Eq><FieldRef Name=\"LeaveReferenceNo\" /><Value Type=\"Text\">" + LeaveRefID + "</Value></Eq></Where>";

                SPListItem lstDatas = obj.GetSPListItem(SiteURL, MainLstName, camlQry);
                if (lstDatas != null)
                {
                    RequestType = Convert.ToString(lstDatas["RequestType"]);
                    lblRequestType.Text = RequestType;
                    lblHRReqNo.Text = Convert.ToString(lstDatas["Reference Number"]);
                    dtpDate.Text = Convert.ToDateTime(lstDatas["RequestDate"]).ToString("dd/MM/yyyy");

                    RefNo =Convert.ToString(lstDatas["Reference Number"]);

                    if (RequestType == "Exit Permit for Leave")
                    {
                        LeaveRefID = Convert.ToString(lstDatas["LeaveReferenceNo"]);
                        txtSponsorNo.Text = Convert.ToString(lstDatas["SponsorNo"]);
                        txtSponsorBranch.Text = Convert.ToString(lstDatas["SponsorshipBranch"]);
                    }
                    txtReason.Text = Convert.ToString(lstDatas["Reason"]);

                    SetExitControls(lstDatas);

                    if (RequestType == "Exit Permit for Leave")
                        pnlReqEmpInf.Visible = true;
                    else
                        pnlReqEmpInf.Visible = false;

                    fldUser = new SPFieldUserValue(SPContext.Current.Web, Convert.ToString(lstDatas["SubmittedFor"]));
                    txtReqUser.Text = fldUser.LookupValue;

                    obj.GetApprovedUsers(RefNo, SiteURL, TaskLstName, gvApprovers);
                }
            }
            catch (Exception ex)
            {
                ErrorMsgs = "Trig_AMJ_HR_Forms" + " :: " + "Parent" + " :: " + "ReLoadDetails";
                obj.GenerateExceptionLog(ErrorMsgs, ex.Message, "");
            }
        }

        #endregion

        private void SetExitControls(SPListItem oItems)
        {
            txtReqEmpId.Text = Convert.ToString(oItems["EmployeeID"]);
            txtReqDivision.Text = Convert.ToString(oItems["Division"]);
            txtReqDesignation.Text = Convert.ToString(oItems["Designation"]);
            txtNationality.Text = Convert.ToString(oItems["Nationality"]);
            dtTravelDate.Text = Convert.ToDateTime(oItems["TravelDate"]).ToString("dd/MM/yyyy");
            dtReturnDate.Text = Convert.ToDateTime(oItems["ReturnDate"]).ToString("dd/MM/yyyy");
            txtLeaveRefNo.Text = Convert.ToString(oItems["Reference_x0020_Number"]);
            dtLeaveReqDate.Text = Convert.ToDateTime(oItems["RequestDate"]).ToString("dd/MM/yyyy");
        }

    }
}